class UserService {
    constructor() {
      this.currentUser = null
    }
  
    setToken(token) {
        console.log('setToken-token:::' + token);
      localStorage.setItem('token', token)
      // 解析token中的用户信息
      try {
        const payload = JSON.parse(atob(token.split('.')[1]))
        this.currentUser = {
          nick_name: payload.nick_name,
          uid: payload.uid,
          head_img: payload.head_img,
          // 可以添加其他需要的用户信息
        }
        localStorage.setItem('user', JSON.stringify(this.currentUser))
      } catch (error) {
        console.error('Token解析失败:', error)
      }
    }
  
    getUser() {
      if (!this.currentUser) {
        const userStr = localStorage.getItem('user')
        if (userStr) {
          this.currentUser = JSON.parse(userStr)
        }
      }
      return this.currentUser
    }
  
    clearUser() {
      localStorage.removeItem('token')
      localStorage.removeItem('user')
      this.currentUser = null
    }
  
    isAuthenticated() {
      return !!this.getUser()
    }
  }
  
  export const userService = new UserService()